package org.jackysoft.interceptor;

import java.util.Map;

import org.jackysoft.struts2.AbstractAction;

import com.google.common.collect.Maps;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;

public class CRUDBusinessInterceptor extends AbstractBiznessInterceptor {

	private static final Map<ActionMethodType, Class<? extends AbstractInterceptor>> interceptor = Maps
			.newHashMap();
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 6582137667685361787L;
	//registe interceptors for action method many to many
	static {
		interceptor.put(ActionMethodType.get, GetInterceptor.class);
		interceptor.put(ActionMethodType.getone, GetInterceptor.class);
		interceptor.put(ActionMethodType.gets, GetsInterceptor.class);
		interceptor.put(ActionMethodType.create, CreateInterceptor.class);
		interceptor.put(ActionMethodType.creates, BatchCreateInterceptor.class);
		interceptor.put(ActionMethodType.add, CreateInterceptor.class);
		interceptor.put(ActionMethodType.remove, RemoveInterceptor.class);
		interceptor.put(ActionMethodType.removes, BatchRemoveInterceptor.class);
		interceptor.put(ActionMethodType.update, UpdateInterceptor.class);
		interceptor.put(ActionMethodType.updates, BatchUpdateInterceptor.class);
		interceptor.put(ActionMethodType.sorted, SortInterceptor.class);
		interceptor.put(ActionMethodType.list, PagerInterceptor.class);
		interceptor.put(ActionMethodType.select, PagerInterceptor.class);
		interceptor.put(ActionMethodType.query, QueryInterceptor.class);
		interceptor.put(ActionMethodType.details, DetailsInterceptor.class);
		interceptor.put(ActionMethodType.upload, UploadInterceptor.class);
		interceptor.put(ActionMethodType.uploads, UploadsInterceptor.class);
		interceptor.put(ActionMethodType.pager, PagerInterceptor.class);
		interceptor.put(ActionMethodType.download, DownloadInterceptor.class);
		interceptor.put(ActionMethodType.ztree, ZtreeInterceptor.class);
		interceptor.put(ActionMethodType.dataimport, DataImportInterceptor.class);
		interceptor.put(ActionMethodType.options, OptionsInterceptor.class);
		interceptor.put(ActionMethodType.savestream, SerlvetInterceptor.class);
	}
	

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub

	}

	@Override
	public String intercept(ActionInvocation invokor) throws Exception {

		ActionProxy proxy = invokor.getProxy();
		AbstractAction action = (AbstractAction) proxy.getAction();
		if (!action.isMapDefaultActionMethod() || !action.isMapDefault()) {
			return invokor.invoke();
		}
		String methodName = proxy.getMethod();
		logger.info("Action was intercepted by "+ methodName+" method");
		ActionMethodType amt = ActionMethodType.parse(methodName);
		if (amt == ActionMethodType.unknow)
			return invokor.invoke();
		Class<? extends AbstractInterceptor> cls = interceptor.get(amt);
		AbstractInterceptor acti = cls.newInstance();
		logger.info("Action was executed by " + acti.getClass().getName());
		return acti.intercept(invokor);
	}
}
